package com.lantosec.hmrp.modules.sysdata.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lantosec.hmrp.modules.sysdata.entity.SysDataModel;
import com.lantosec.hmrp.modules.sysdata.mapper.SysDataModelMapper;
import com.lantosec.hmrp.modules.sysdata.service.ISysDataModelService;
import com.lantosec.hmrp.modules.sysdata.vo.FieldVO;
import lombok.extern.log4j.Log4j2;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 系统数据模型定义表 服务实现类
 * </p>
 *
 * @author AutoCode
 * @since 2020-03-02
 */
@Service
@Primary
@Log4j2
public class SysDataModelServiceImpl extends ServiceImpl<SysDataModelMapper, SysDataModel> implements ISysDataModelService {

    @Override
    public boolean save(SysDataModel entity) {
        entity.setCreateTime(new Date());
        entity.setEditTime(new Date());
        return super.save(entity);
    }

    @Override
    public List<FieldVO> selectTableFields(String dataModelId) {
        SysDataModel model = getById(dataModelId);
        List<Map<String, String>> data = baseMapper.selectTableFields(model.getDatabaseName(), model.getTableName());
        return data.parallelStream().map(item -> {
            return new FieldVO(item.get("Field"), item.get("Type"), item.get("Comment"), "YES".equals(item.get("Null")), "PRI".equals(item.get("Key")), false);
        }).collect(Collectors.toList());
    }
}
